package com.lxmes.gs.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.gs.domain.MyTask;
import com.lxmes.gs.service.IMyTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxmes.gs.mapper.TaskIssueMapper;
import com.lxmes.gs.domain.TaskIssue;
import com.lxmes.gs.service.ITaskIssueService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 任务下发Service业务层处理
 *
 * @author wdf
 * @date 2023-04-24
 */
@Service
public class TaskIssueServiceImpl extends ServiceImpl<TaskIssueMapper, TaskIssue> implements ITaskIssueService {

    @Autowired
    private TaskIssueMapper taskIssueMapper;
    @Autowired
    protected Validator validator;
    @Autowired
    private IMyTaskService myTaskService;
    /**
     * 查询任务下发
     *
     * @param id 任务下发主键
     * @return 任务下发
     */
    @Override

    public TaskIssue selectTaskIssueById(Long id) {
        return taskIssueMapper.selectById(id);
    }

    /**
     * 查询任务下发列表
     *
     * @param taskIssue 任务下发
     * @return 任务下发
     */
    @Override
    public List<TaskIssue> selectTaskIssueList(TaskIssue taskIssue) {
        return taskIssueMapper.selectList(Wrappers.lambdaQuery(taskIssue));
    }

    /**
     * 新增任务下发
     *
     * @param taskIssue 任务下发
     * @return 结果
     */
    @Override
    public int insertTaskIssue(TaskIssue taskIssue) {
        taskIssue.setCreateTime(DateUtils.getNowDate());
        return taskIssueMapper.insert(taskIssue);
    }

    /**
     * 修改任务下发
     *
     * @param taskIssue 任务下发
     * @return 结果
     */
    @Override
    public int updateTaskIssue(TaskIssue taskIssue) {
        return taskIssueMapper.updateById(taskIssue);
    }

    /**
     * 批量删除任务下发
     *
     * @param ids 需要删除的任务下发主键
     * @return 结果
     */
    @Override
    public int deleteTaskIssueByIds(Long[] ids) {
        return taskIssueMapper.deleteTaskIssueByIds(ids);
    }

    /**
     * 删除任务下发信息
     *
     * @param id 任务下发主键
     * @return 结果
     */
    @Override
    public int deleteTaskIssueById(Long id) {
        return taskIssueMapper.deleteById(id);
    }

    @Override
    public String imporIteminfo(List<TaskIssue> taskIssueList, boolean updateSupport) {
        if (StringUtils.isNull(taskIssueList) || taskIssueList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TaskIssue taskIssue : taskIssueList) {
            try {
                BeanValidators.validateWithException(validator, taskIssue);
                this.insertTaskIssue(taskIssue);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int auditTaskIssue(Long[] ids) {
        List<TaskIssue> taskIssues = baseMapper.selectBatchIds(Arrays.asList(ids));
        if (taskIssues.stream().anyMatch(i -> StrUtil.equals(i.getAudit(), "1"))) {
            throw new ServiceException("存在已审核数据，操作失败");
        }
        for (TaskIssue taskIssue : taskIssues) {
            taskIssue.setAudit("1");
            this.updateTaskIssue(taskIssue);
            MyTask myTask = BeanUtil.copyProperties(taskIssue, MyTask.class);
            myTaskService.insertMyTask(myTask);
        }
        return 1;
    }
}
